export class PlayerPlane extends Phaser.Physics.Arcade.Sprite {
  // 玩家飞机类
  constructor(scene) {
    // x y 坐标 初始值  
    let x = scene.gameCenterX
    let y = scene.gameCenterY + 100
    super(scene, x, y, 'player')
    scene.physics.add.existing(this) // 加入物理组
    scene.add.existing(this) // 加入场景
    this._init()
  }

  bulletDelay = null // 子弹发射定时器
  bullets = null // 子弹组
  buffTimers = [] // 飞机buff组
  bullet = {
    quantity: 1, // 子弹连发数量
    space: 15, // 子弹间隔
    reloadSpeed: 300, // 装弹间隔
    speed: 300, // 子弹飞行速度
    damage: 1, // 子弹威力
    extraScale: 0 // 子弹额外体积 %
  }

  // 初始化
  _init() {
    this.setScale(0.8, 0.8)
    this.setInteractive()
    this.setCollideWorldBounds() // 设置碰撞边界
    // this.setGravityY(0) // y轴重力

    this._initAnims()
    this._initBullet()
    this.reset()

    this.drag = false
    // 监听拖动
    this.scene.input.on('pointerdown', (e) => {
      this.drag = true
    })
    this.scene.input.on('pointerup', (e) => {
      this.drag = false
    })
    this.scene.input.on('pointermove', (e) => {
      if (!this.drag || this.scene.gameOver) return
      // 拖曳式操控
      this.x += e.x - e.prevPosition.x
      this.y += e.y - e.prevPosition.y
    })
  }
  // 发射子弹
  createBullet() {
    let leftX = this.x - (this.bullet.quantity - 1) * (this.bullet.space / 2)
    // 确定最左边位置的子弹X 再依次加间隔生成
    for (let i = 0; i < this.bullet.quantity; i++) {
      // get 有则获取 无则创建 
      let bullet = this.bullets.get(leftX, this.y - this.height / 2, 'bomb')
      leftX += this.bullet.space
      bullet.name = 'bullet'
      bullet.damage = this.bullet.damage
      bullet.setVelocity(0, -this.bullet.speed) // 设置y轴方向
      bullet.setScale(0.5 + this.bullet.extraScale, 0.5 + this.bullet.extraScale)
      bullet.setActive(true)
      bullet.setVisible(true)
    }
  }
  // 子弹打中敌机
  enemyAndMyBulletCollision(bullet, enemyPlane) {
    if (bullet.active && enemyPlane.active && enemyPlane.hp > 0) {
      // 消除子弹
      enemyPlane.hp -= bullet.damage
      bullet.setPosition(-100, -100)
      bullet.setVelocity(0, 0)
      this.bullets.killAndHide(bullet)
      this.scene.enemyPlanes.killEnemy(enemyPlane)
    }
  }
  // 碰撞到敌机 
  enemyAndPlayerCollision(plane, enemyPlane) {
    if (this.scene.gameOver) return
    this.anims.play('planeBoom')
    this.scene.playerFail()
  }
  // 暂停
  pause() {
    this.bullets.children.iterate((child) => {
      child.setVelocity(0, 0)
    }, this)
  }
  // 重置
  reset() {
    let x = this.scene.gameCenterX
    let y = this.scene.gameCenterY + 100
    this.setPosition(x, y)
    this.anims.play('planeRun', true)
    this.bullets.children.iterate((child) => {
      child.setPosition(-100, -100)
      this.bullets.killAndHide(child)
    })
    this.buffTimers.forEach(child => clearTimeout(child))
    this.buffTimers = []

    // 玩家飞机配置
    this.bullet = {
      quantity: 1, // 子弹连发数量
      space: 15, // 子弹间隔
      reloadSpeed: 300, // 装弹间隔
      speed: 300, // 子弹飞行速度
      damage: 1, // 子弹威力
      extraScale: 0 // 子弹额外体积 %
    }
    this.bulletDelay.delay = this.bullet.reloadSpeed
  }
  // 捡到buff道具
  playerBuff(player, buff) {
    // 消耗道具 
    if (this.scene.audio) this.scene.sounds.getBuff.play()
    buff.setVelocity(0, 0)
    buff.setPosition(-100, -100)
    this.scene.propSupply.buffs.killAndHide(buff)

    // 随机生成一个buff
    let n = Math.random()
    let timer = null
    if (n <= 0.4) {
      // 40% 子弹+1
      this.scene.show('弹夹扩容', player.x, player.y)
      this.bullet.quantity += 1
      timer = setTimeout(() => {
        this.bullet.quantity -= 1
        this.buffTimers.splice(this.buffTimers.indexOf(timer), 1)
      }, buff.duration);

    } else if (n <= 0.7) {
      // 30% 射速加快 0.6
      this.scene.show('射速提升', player.x, player.y)
      this.bullet.reloadSpeed *= 0.6
      this.bulletDelay.delay = this.bullet.reloadSpeed
      timer = setTimeout(() => {
        this.bullet.reloadSpeed /= 0.6
        this.bulletDelay.delay = this.bullet.reloadSpeed
        this.buffTimers.splice(this.buffTimers.indexOf(timer), 1)
      }, buff.duration);
    } else if (n <= 1) {
      // 30% 子弹升级 速度加50 威力+1
      this.scene.show('子弹升级', player.x, player.y)
      this.bullet.damage += 1
      this.bullet.speed += 50
      this.bullet.extraScale += 0.2
      timer = setTimeout(() => {
        this.bullet.damage -= 1
        this.bullet.speed -= 50
        this.bullet.extraScale -= 0.2
        this.buffTimers.splice(this.buffTimers.indexOf(timer), 1)
      }, buff.duration);
    }
    if (timer) this.buffTimers.push(timer)
  }
  update() {
    // 子弹更新回收
    this.bullets.children.iterate((child) => {
      if (child.y < 0) {
        this.bullets.killAndHide(child)
      }
    })
  }
  // 初始化动画
  _initAnims() {
    // 正常运行动画
    this.scene.anims.create({
      key: 'planeRun',
      frames: this.scene.anims.generateFrameNumbers('player', {
        start: 0,
        end: 1
      }),
      frameRate: 8,
      repeat: -1
    })
    // 爆炸动画
    this.scene.anims.create({
      key: 'planeBoom',
      frames: this.scene.anims.generateFrameNumbers('player', {
        start: 1,
        end: 4
      }),
      frameRate: 8,
      repeat: -1
    })
  }

  // 初始化子弹
  _initBullet() {
    this.bullets = this.scene.physics.add.group()
    // 自动发射子弹
    this.bulletDelay = this.scene.time.addEvent({
      delay: this.bullet.reloadSpeed,
      loop: true,
      callback: () => {
        if (!this.scene.gameOver) this.createBullet()
      }
    })
    // 子弹碰撞敌机检测
    this.scene.physics.add.overlap(this.bullets, this.scene.enemyPlanes.smallPlanes, this.enemyAndMyBulletCollision, false, this)
    this.scene.physics.add.overlap(this.bullets, this.scene.enemyPlanes.midPlanes, this.enemyAndMyBulletCollision, false, this)
    this.scene.physics.add.overlap(this.bullets, this.scene.enemyPlanes.bigPlanes, this.enemyAndMyBulletCollision, false, this)

    // 机体碰撞敌机检测
    this.scene.physics.add.overlap(this, this.scene.enemyPlanes.smallPlanes, this.enemyAndPlayerCollision, false, this)
    this.scene.physics.add.overlap(this, this.scene.enemyPlanes.midPlanes, this.enemyAndPlayerCollision, false, this)
    this.scene.physics.add.overlap(this, this.scene.enemyPlanes.bigPlanes, this.enemyAndPlayerCollision, false, this)

    // 机体碰撞道具检测
    this.scene.physics.add.overlap(this, this.scene.propSupply.bombs, this.scene.enemyPlanes.killAllEnemys, false, this.scene.enemyPlanes)
    this.scene.physics.add.overlap(this, this.scene.propSupply.buffs, this.playerBuff, false, this)
  }
}
